package tcom.huitone.smspfm.dao.redis;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.bin.common.utils.DateTimeUtils;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.context.utils.ContextUtils;
import com.huitone.smspfm.redis.impl.HashOpts;
import com.huitone.smspfm.redis.impl.ListOpts;
import com.huitone.smspfm.redis.impl.ZsetOpts;
import com.huitone.smspfm.redis.pipeline.PipelineOpts;
import com.huitone.smspfm.redis.pipeline.PipelineOpts.ABExcuteCommands;

public class WriteData {

	@Test
	public void write() {
		
		PipelineOpts pipelineOpts = ContextUtils.getBean2(PipelineOpts.class);
		
		long timeMillisStart = System.currentTimeMillis();
		
		for (int i = 0 ; i < 1 ; i ++) {
			
			pipelineOpts.save(new ABExcuteCommands() {
				
				@Override
				public void excute(RedisConnection connection) {
					for (int j = 0 ; j < 100 ; j ++) {
						
						//15676882344
						String iphoneNum = (13560027887L + j) + "";
						
						long a = 2000001;
						long b = StringUtils.tolong(DateTimeUtils.dateToStr(new Date(), "MMddHHmmss"));
						long c = j;
						
						String sequence = a + "" + b + "" + c;
						
						String key = "SMInfo:" + sequence + ":" + iphoneNum;			
						
						Map<byte[], byte[]> map = new HashMap<>();
						StringRedisSerializer srs = new StringRedisSerializer();
						map.put(srs.serialize("Srv_node"), srs.serialize("100002"));
						map.put(srs.serialize("ServiceCode"), srs.serialize("1000021"));
						map.put(srs.serialize("SmType"), srs.serialize("1"));
						map.put(srs.serialize("User"), srs.serialize(iphoneNum + ""));
						map.put(srs.serialize("Smcont"), srs.serialize("下行短信处理测试：从列表中弹出值，将其推送到另一个列表并返回它; 或，直到一个可用；Redis列表只是字符串列表，按插入顺序排序。可以在列表的头部或尾部添加Redis列表中的元素。列表的最大长度为2^32 - 1个元素(即4294967295，每个列表可存储超过40亿个元素4040" + j));
						map.put(srs.serialize("Reportflag"), srs.serialize("2"));
						map.put(srs.serialize("recvTime"), srs.serialize(DateTimeUtils.dateToStr2(new Date())));
						map.put(srs.serialize("RecvModuleName"), srs.serialize("test"));
						map.put(srs.serialize("SerialSegment"), srs.serialize(a + "," + b + "," + c));
						map.put(srs.serialize("SendPort"), srs.serialize("8812"));

						connection.hMSet(srs.serialize(key), map);
						connection.lPush(srs.serialize("SMCache"), srs.serialize(sequence + ":" + iphoneNum));
					}
					
				}
			});
		}
		
		long timeMillisEdn = System.currentTimeMillis();
		
		System.out.println(timeMillisEdn - timeMillisStart);
	}
	
	@Test
	public void writeMo() {
		
		ListOpts listOpts = (ListOpts) ContextUtils.getBean2("listOpts3");
		
		HashOpts hashOpts = (HashOpts) ContextUtils.getBean2("hashOpts3");
		
		long timeMillisStart = System.currentTimeMillis();
		
		for (int i = 0 ; i < 1 ; i ++) {
			//15676882344
			String iphoneNum = (13500000000L + i) + "";
			
			long a = 1000001;
			long b = StringUtils.tolong(DateTimeUtils.dateToStr(new Date(), "MMddHHmmss"));
			long c = i;
			
			String sequence = a + "" + b + "" + c;
			
			String key = "SMInfo:" + sequence + ":" + iphoneNum;			
			
			Map<String, Object> map = new HashMap<>();
			map.put("SmType", "2");
			map.put("User", iphoneNum);
			map.put("Smcont", "M");
			map.put("recvTime", DateTimeUtils.dateToStr2(new Date()));
			map.put("RecvModuleName", "1");
			map.put("SerialSegment", a + "," + b + "," + c);
			map.put("SendPort", "106907190955988814");
			map.put("GateLinkId", "1");

			hashOpts.put(key, map);
			
			listOpts.in("SMMoGateList", sequence + ":" + iphoneNum);
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			System.out.println(i);
		}
		
		long timeMillisEdn = System.currentTimeMillis();
		
		System.out.println(timeMillisEdn - timeMillisStart);
	}
	
	@Test
	public void writeForbidList() {
		
		ZsetOpts setOpts = ContextUtils.getInstance().getBean(ZsetOpts.class);
		
		
		setOpts.add("testscan", "sssssss:13560027887", 0.1);
		setOpts.add("testscan", "hjjjjhhh:13560027887", 0.1);
		setOpts.add("testscan", "gggggggg:13560027887", 0.1);
		
		
		Cursor<TypedTuple<String>> cursor = setOpts.scan("testscan", ScanOptions.scanOptions().match("*13560027887").build());
		
		while(cursor.hasNext()) {
			TypedTuple<String> next = cursor.next();
			
			System.out.println(next.getValue());
		}
		
		System.out.println("写入完成！");
	}
	
}
